<?php


namespace app\agent\controller;

use app\common\model\JumpTypeMap;
use app\common\service\agent\showBrandByJumpTypeService;
use app\common\service\takeout\JtkTakeoutService;
use app\model\PageSetting as PageSettingModel;
use app\agent\validate\PageSetting as PageSettingValidate;
use Carbon\Carbon;
use think\Request;
use app\common\facade\auth\Auth;

class PageSetting extends Controller
{

    /**
     * 首页列表
     * @access public
     * @method get
     * @return \think\response\Json
     */
    public function list(Request $request, PageSettingValidate $validate)
    {
        $param = $request->param();
        if (!$validate->scene('list')->check($param)) {
            return fail($validate->getError());
        }
        $list = (new PageSettingModel())->getList($param);
        return success('success', $list);
    }

    /**
     * 新增
     * @access public
     * @method post
     * @return \think\response\Json
     */
    public function save(Request $request, PageSettingValidate $validate)
    {
        $params = $request->param();
        if (isset($params['id']) && !empty($params['id'])) {
            $scene = 'update';
        } else {
            $scene = 'create';
        }

        if (!$validate->scene($scene)->check($params)) {
            return fail($validate->getError());
        }
        if (!empty($params['jump_type']) && $params['jump_type'] > 0 && empty($params['path'])) {
            return fail('请输入H5链接地址');
        }
        $id = (new PageSettingModel())->doSave($params);
        return success('操作成功',   [
            'id' => intval($id),
        ]);
    }

    /**
     * 修改状态
     * @access public
     * @method post
     * @return \think\response\Json
     */
    public function status(Request $request, PageSettingValidate $validate)
    {
        $data = $request->param();
        if (!$validate->scene('status')->check($data)) {
            return fail($validate->getError());
        }
        /**
         * @var PageSettingModel $pageSetting
         */
        $pageSetting = PageSettingModel::query()->where('id', $data['id'])->find();
        if (!$pageSetting) {
            return fail('数据不存在');
        }
        if ($data['status'] == PageSettingModel::SOURCE_SYSTEM
            && !empty($pageSetting->act_end_date)
            && strtotime($pageSetting->act_end_date)
            && Carbon::parse($pageSetting->act_end_date)->endOfDay()->isPast()
        ) {
            return fail('活动已失效');
        }
        $pageSetting->status = $data['status'];
        $pageSetting->save();
        return success('状态修改成功', [
            'id' => $data['id'],
        ]);
    }

    /**
     * 删除
     * @access public
     * @method post
     * @return \think\response\Json
     */
    public function delete(Request $request, PageSettingValidate $validate)
    {
        $data = $request->param();
        if (!$validate->scene('delete')->check($data)) {
            return fail($validate->getError());
        }
        /**
         * @var PageSettingModel $pageSetting
         */
        $pageSetting = PageSettingModel::query()->where('id', $data['id'])->find();
        if (!$pageSetting) {
            return fail('数据不存在或者已删除');
        }
        $pageSetting->delete();
        return success('删除成功', [
            'id' => $data['id'],
        ]);
    }

    /**
     * 同步系统配置
     * @access public
     * @method get
     * @return \think\response\Json
     */
    public function sync(Request $request, PageSettingValidate $validate)
    {
        $params = $request->param();
        if (!$validate->scene('sync')->check($params)) {
            return fail($validate->getError());
        }
        $params['agent_id'] = Auth::agentId();
        return (new PageSettingModel())->sysnc($params);
    }


    /**
     * 删除
     * @access public
     * @method get
     * @return \think\response\Json
     */
    public function showBrandByJumpType(Request $request)
    {
        $jump_type = $request->param('jump_type');
        $brandName = $request->param('brandName')??'';
        return (new showBrandByJumpTypeService())->showBrandByJumpType($jump_type, $brandName);
    }

    /**
     * 获取跳转链接列表
     */
    public function getJumpTypeList(Request $request)
    {
        $template_url = $request->param('template_url');
        return (new JumpTypeMap())->getJumpTypeList($template_url);
    }

    /**
     * 美团券包所有
     * @return \think\response\Json
     */
    public function getCouponAdAll() {
        $data = (new JtkTakeoutService())->meituanCouponAdAll();
        return success('success', $data);
    }
}